/**
 * ReportGear(2011)
 */
package com.reportgear.designer.style.pane;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Enumeration;
import java.util.List;

import javax.swing.AbstractButton;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

import com.reportgear.core.swing.pane.base.JBasePane;
import com.reportgear.designer.pane.style.ICellStyle;
import com.reportgear.designer.ui.support.JListUtils;
import com.reportgear.report.model.cell.Cell;
import com.reportgear.report.model.cell.style.format.FormatterFactory;
import com.reportgear.report.model.cell.style.format.FormatterFactory.FormatterKey;
import com.reportgear.report.view.JWorksheet;

/**
 * @version 1.0 2011-3-24
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since Report 1.0
 */
public class FormatPane extends JBasePane implements ICellStyle {
	/**
	 * UID
	 */
	private static final long serialVersionUID = 1155363628241214623L;
	private FormatterFactory formatFactory = FormatterFactory.INSTANCE;
	private JList formatList;
	private ButtonGroup group;

	public FormatPane() {
		super();

		initComponents();
	}

	private void initComponents() {
		FormatActionListener action = new FormatActionListener();

		JPanel pnlSort = new JPanel();
		pnlSort.setLayout(new BoxLayout(pnlSort, BoxLayout.Y_AXIS));
		pnlSort.setBorder(BorderFactory.createTitledBorder("分类"));
		pnlSort.setPreferredSize(new Dimension(100, 100));
		add(pnlSort, BorderLayout.WEST);

		List<String> sorts = formatFactory.getFormatTypes();
		group = new ButtonGroup();
		for (String text : sorts) {
			JRadioButton btnSort = new JRadioButton(text);
			btnSort.addActionListener(action);
			pnlSort.add(btnSort);

			group.add(btnSort);
		}

		JPanel pnlContent = new JPanel();
		pnlContent.setBorder(BorderFactory.createTitledBorder("格式"));
		add(pnlContent, BorderLayout.CENTER);
		pnlContent.setLayout(new BorderLayout());
		formatList = new JList();
		pnlContent.add(formatList, BorderLayout.CENTER);

		AbstractButton curBtn = group.getElements().nextElement();
		if (curBtn != null) {
			curBtn.doClick();
		}
	}

	/**
	 * 获取选择的格式
	 * 
	 * @return 格式串
	 */
	public String getFormatterKey() {
		Object o = formatList.getSelectedValue();

		if (o != null) {
			return o.toString();
		} else {
			return "";

		}
	}

	/**
	 * 设置格式串
	 * 
	 * @param formatStr
	 *            格式化串
	 */
	public void setFormatString(String formatStr) {
		int sort = formatFactory.getKeyType(formatStr);
		Enumeration<AbstractButton> btns = group.getElements();
		AbstractButton btn = null;

		for (int i = 0; i <= sort; i++) {
			if (btns.hasMoreElements()) {
				btn = btns.nextElement();
			}
		}

		if (btn != null) {
			btn.doClick();
			formatList.setSelectedValue(formatStr, true);
		}
	}

	public void populateCellStyle(JWorksheet workSheet) {
		if (getFormatterKey().equals(workSheet.getActiveCellStyle().getFormatterKey())) {
			return;
		}

		setFormatString(workSheet.getActiveCellStyle().getFormatterKey());
	}

	public void updateCellStyle(JWorksheet workSheet) {
		List<Cell> cells = workSheet.getSelectedCells();
		for (Cell cell : cells) {
			cell.setStyle(cell.getStyle().deriveFormatterKey(this.getFormatterKey()));
		}
	}

	// 选择格式Action监听
	private class FormatActionListener implements ActionListener {

		public void actionPerformed(ActionEvent e) {
			List<FormatterKey> formatCaptions = FormatterFactory.INSTANCE.getFormattersByCaption(((JRadioButton) e
					.getSource()).getText());
			JListUtils.setJListValueList(formatList, formatCaptions);
		}
	}

	public static void main(String[] args) {
		FormatPane pane = new FormatPane();
		pane.showDialog(null, "测试", 400, 300);
	}
}
